package main

import "fmt"

func main() {
	testGoChan()
}

func testGoChan() {
	done := make(chan struct{})
	ch := make(chan int)

	//go goWithRange(done, ch)
	go goWithOk(done, ch)

	for i := 0; i < 3; i++ {
		ch <- i
	}

	close(ch)
	<-done
}

func goWithRange(done chan struct{}, ch chan int) {
	defer close(done)

	for v := range ch {
		fmt.Println("get:", v)
	}
}

func goWithOk(done chan struct{}, ch chan int) {
	defer close(done)

	for {
		v, ok := <-ch
		if !ok {
			return
		}

		fmt.Println("get:", v)
	}
}
